%%%  Script to generate simulations of resubmission decisions, resubmission costs, and government outlays
%%%  varying the fees and probability of denial

clear all
import_for_simulations

%%%% PARAMETERS for SIMULATIONS
beta = 0.99; % discount factor (see footnote 21)
kappa =13.87;      % average resubmission cost (see Figure 4)
linespervisit = 1.82; % average number of line items per visit (see Table 1)
btau = -0.0779;   % -0.1482 (regression coefficients from Table 6, column 4)
bfee = 0.0285;   % 0.1116 (regression coefficients from Table 6, column 4)

%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% BASELINE SIMULATION
V0 =  line_item_value.*rec_rate;
V=0;
Vstar = V0;
while max(abs(Vstar-V))>0.001 
    V = Vstar;  
    Vstar = max( 0 , beta*( rec_rate.*line_item_value + (1-rec_rate).*V )-kappa);  
    disp(max(abs(Vstar-V)))
end

baseCPU = line_item_value - (1-D_0).*line_item_value - D_0 .* Vstar;
basetau = baseCPU./line_item_value;

DF = -.2:.002:.2;
DD = -.3:.002:.3;
[FEE,DENIAL]=meshgrid(DF,DD);

TAU = zeros(size(DENIAL));
ACCEPT = zeros(size(DENIAL));
GOV = zeros(size(DENIAL));
COSTS = zeros(size(DENIAL));

V0 =  line_item_value.*rec_rate;

for df = DF
    for dd = DD
        
        LIV = line_item_value*(1+df);
        DEN = min(1,D_0*(1+dd));
        REC = 1-min(1,(1-rec_rate)*(1+dd));

        V=0;
        Vstar = V0;
        while max(abs(Vstar-V))>0.001 
            V = Vstar;  
            Vstar = max( 0 , beta*( REC.*LIV+ (1-REC).*V )-kappa);  
        end
        sigma = (beta*( REC.*LIV+ (1-REC).*Vstar )-kappa) >0;
        
        CPU = LIV - (1-DEN).*LIV- DEN .* Vstar;
        tau = CPU./LIV;
        gov = (1-DEN).*LIV + DEN.*sigma.*LIV;
        costs = gov-(LIV-CPU);
        
        TAU(DD==dd,DF==df)= sum(obs.*tau)/sum(obs);
        
        ACCEPT(DD==dd,DF==df)= ...
            log(1+df)*bfee + (TAU(DD==dd,DF==df)-sum(obs.*basetau)/sum(obs))*btau ;
        
        GOV(DD==dd,DF==df) = linespervisit*sum(obs.*gov)/sum(obs);
        COSTS(DD==dd,DF==df) = linespervisit*sum(obs.*costs)/sum(obs);
            
       
    end
end

%%%% subplot 1 is tau response to fee and denial
figure;
contour(FEE*100,DENIAL*100,TAU,'EdgeColor','k','ShowText','on');
xlabel('% change in \pi') 
ylabel('% change in denial probability') 
ax = gca;
ax.FontSize = 15;
txt = {'     \uparrow     ' , 'Change in \tau'};
text(5,11.5,txt)
print -dpdf tau.pdf

%%%% subplot 2 is iso-acceptance with iso-government and labels
figure;
contour(FEE*100,DENIAL*100,ACCEPT,[0,0],'EdgeColor','k','LineWidth',1);
hold on
contour(FEE*100,DENIAL*100,GOV-GOV(DD==0,DF==0),'EdgeColor','k','ShowText','on','LineStyle','--');
xlabel('% change in \pi') 
ylabel('% change in denial probability') 
ax = gca;
ax.FontSize = 15;
txt = {'\leftarrow Constant' , '     Medicaid acceptance'};
text(3.6,5,txt)
txt1 = {'Change in per-visit \rightarrow' , 'payments to physicians'};
text(-10,20,txt1)
print -dpdf acceptbudget.pdf